package web.admin.shop.category;

import domain.shop.Category;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import service.facade.admin.ShopAdminService;
import web.admin.components.TinyMceBehavior;
import web.eshop.tree.CategoryTreeModelContainer;

import javax.swing.tree.DefaultMutableTreeNode;
import java.util.MissingResourceException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Provides settings of particular category
 */
public final class CategoryPanel extends Panel {

    @SpringBean
    private ShopAdminService shopAdminService;
    @SpringBean
    CategoryTreeModelContainer categoryTreeModelContainer;
    private final IModel<Category> categoryModel;
    private static Logger logger = Logger.getLogger(CategoryPanel.class.getName());

    public CategoryPanel(String id, final DefaultMutableTreeNode categoryNode, final CategoryAdminPanel parent) {
        super(id);
        final Long categoryId = ((Category) categoryNode.getUserObject()).getId();
        setOutputMarkupId(true);
        categoryModel = new LoadableDetachableModel<Category>() {

            @Override
            protected Category load() {
                return shopAdminService.findCategoryById(categoryId);
            }
        };

        initPageEditForm(categoryModel);

        add(new Form("actions") {

            {
                add(new Button("addChild") {

                    @Override
                    public void onSubmit() {
                        Category child = null;
                        try {
                            child = shopAdminService.createCategoryAsChild(categoryModel.getObject());

                            CategoryLeftMenuAdminPanel panel = (CategoryLeftMenuAdminPanel) parent.getAdminPage().getLeftPanel();
                            DefaultMutableTreeNode childNode = panel.addNode(categoryNode, child);
                            parent.replacePanel(childNode, null);

                            info(getLocalizer().getString("shop.success.category.child.add", parent));
                        } catch (Exception e) {
                            logger.log(Level.SEVERE, null, e);
                            error(getLocalizer().getString("system.error", this));
                        }

                    }
                });
                add(new Button("delete") {

                    @Override
                    public void onSubmit() {
                        try {
                            shopAdminService.deleteCategory(categoryModel.getObject());

                            CategoryLeftMenuAdminPanel panel = (CategoryLeftMenuAdminPanel) parent.getAdminPage().getLeftPanel();
                            panel.removeNode(categoryNode);
                            parent.replacePanel();
                            getSession().info(getLocalizer().getString("shop.success.category.delete", CategoryPanel.this));
                        } catch (MissingResourceException e) {
                            error(getLocalizer().getString("system.error", this));
                            logger.log(Level.SEVERE, null, e);

                        }

                    }
                });
            }
        });
    }

    private void initPageEditForm(final IModel<Category> categoryModel) {
        add(new Form<Category>("form", categoryModel) {

            {
                add(new TextField<String>("title", new PropertyModel<String>(categoryModel, "title")).setRequired(true));
                add(new TextField<String>("description", new PropertyModel<String>(categoryModel, "description")));
                add(new TextField<String>("keywords", new PropertyModel<String>(categoryModel, "keywords")));
                TextArea<String> content = new TextArea<String>("content", new PropertyModel<String>(categoryModel, "content"));
                add(content);
                content.add(new TinyMceBehavior(true));
//                content.add(new wicket.contrib.tinymce.TinyMceBehavior());
            }

            @Override
            protected void onSubmit() {
                try {
                    shopAdminService.updateCategory((Category) categoryModel.getObject());
                    info(getLocalizer().getString("shop.success.category.update", this));
                } catch (Exception e) {
                    logger.log(Level.SEVERE, null, e);
                    error(getLocalizer().getString("system.error", this));
                }
            }
        });
    }

}
